package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.domain.bo.PtSysReportBo;
import com.ruoyi.system.domain.vo.PtSysReportVo;
import com.ruoyi.system.domain.PtSysReport;
import com.ruoyi.system.mapper.PtSysReportMapper;
import com.ruoyi.system.service.IPtSysReportService;

import java.util.List;
import java.util.Map;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * 用户实验报告Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-26
 */
@Service
public class PtSysReportServiceImpl extends ServicePlusImpl<PtSysReportMapper, PtSysReport, PtSysReportVo> implements IPtSysReportService {

    @Autowired
    private SysUserMapper userMapper;

    @Override
    public PtSysReportVo queryById(Long id) {
        return getVoById(id);
    }

    @Override
    public TableDataInfo<PtSysReportVo> queryPageList(PtSysReportBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PtSysReport> lqw = buildQueryWrapper(bo);
        Page<PtSysReportVo> result = pageVo(pageQuery.build(), lqw);
        if (result.getRecords() != null) {
            if (bo.getClassId() != null) {
                List<Long> userIds = userMapper.selectUserByClassId(bo.getClassId());
                result.setRecords(result.getRecords().stream().filter(o -> userIds.contains(o.getUserId())).collect(Collectors.toList()));
            }
            if (bo.getUserName() != null || bo.getNickName() != null) {
                List<Long> userIds = userMapper.selectUserByName(bo.getUserName(), bo.getNickName());
                result.setRecords(result.getRecords().stream().filter(o -> userIds.contains(o.getUserId())).collect(Collectors.toList()));
            }
            result.setRecords(result.getRecords().stream().peek(o -> {
                SysUser user = userMapper.selectOne(Wrappers.lambdaQuery(SysUser.class)
                    .eq(SysUser::getUserId, o.getUserId())
                    .select(SysUser::getNickName, SysUser::getUserName)
                );
                o.setNickName(user.getNickName());
                o.setUserName(user.getUserName());
            }).collect(Collectors.toList()));
        }
        return TableDataInfo.build(result);
    }

    @Override
    public List<PtSysReportVo> queryList(PtSysReportBo bo) {
        LambdaQueryWrapper<PtSysReport> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<PtSysReport> buildQueryWrapper(PtSysReportBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PtSysReport> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, PtSysReport::getUserId, bo.getUserId());
        lqw.eq(bo.getExpId() != null, PtSysReport::getExpId, bo.getExpId());
        lqw.eq(StringUtils.isNotBlank(bo.getSubmitUrl()), PtSysReport::getSubmitUrl, bo.getSubmitUrl());
        lqw.eq(bo.getSubmitTime() != null, PtSysReport::getSubmitTime, bo.getSubmitTime());
        lqw.eq(bo.getScorePurpose() != null, PtSysReport::getScorePurpose, bo.getScorePurpose());
        lqw.eq(bo.getScoreTool() != null, PtSysReport::getScoreTool, bo.getScoreTool());
        lqw.eq(bo.getScorePrinciple() != null, PtSysReport::getScorePrinciple, bo.getScorePrinciple());
        lqw.eq(bo.getScoreContent() != null, PtSysReport::getScoreContent, bo.getScoreContent());
        lqw.eq(bo.getScoreData() != null, PtSysReport::getScoreData, bo.getScoreData());
        lqw.eq(bo.getScoreSum() != null, PtSysReport::getScoreSum, bo.getScoreSum());
        lqw.eq(bo.getScoreFinal() != null, PtSysReport::getScoreFinal, bo.getScoreFinal());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), PtSysReport::getStatus, bo.getStatus());
        return lqw;
    }

    @Override
    public Boolean insertByBo(PtSysReportBo bo) {
        PtSysReport add = BeanUtil.toBean(bo, PtSysReport.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(PtSysReportBo bo) {
        PtSysReport update = BeanUtil.toBean(bo, PtSysReport.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(PtSysReport entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
